from django.contrib.auth.models import AbstractUser
from django.db import models
from django.templatetags.static import static


# 继承Django自带的用户抽象类
# 用户信息包含 头像/昵称/简介
class User(AbstractUser):
    avatar = models.ImageField(upload_to='avatars/', blank=True, null=True)
    nickname = models.CharField(max_length=50, blank=True)
    bio = models.TextField(blank=True)

    # 头像设置函数
    def get_avatar_url(self):
        # hasattr 检查对象是否觉有特定属性 -> 检查是否有url属性(即确定是否为图像资源)
        if self.avatar and hasattr(self.avatar,'url'):
            return self.avatar.url
        return static('images/default_avatar.png')